<?php
class UserController extends AppController
{
    public $name = 'User';
    public $helpers = array('Html', 'Form');
    public $components = array('Session');
    public $uses = array(
        'tbl_account', 'tbl_character', 'tbl_billing_log', 'tbl_item', 'tbl_card','tbl_log'
    );
    public $lang_usn_test = '545b16785ae8ac3c1d191e53';

    public function user_list()
    {
        $user_list = '';
        $search_info = '';
        $user_character_list = '';

        /*foreach($this->data as $key=>$value)
        {
            if ( is_array($value))
            {
                echo $key. '<br />' ;
                foreach($value as $key2=>$value2)
                    echo $key2."=>".$value2. '<br />';
            }
            else
                echo $key."=>".$value. '<br />';
        }*/
        if(!empty($this->data['search_user']))
        {
            $search_info = $this->data['search_user'];

            if(!empty($search_info['account']))
            {
                /*if(!$user_list = $this->tbl_account->search_account($search_info['account']))
                {
                    $err_msg = LANG_USER_EMPTY;
                }
                else
                {
                    $num = 1 ;
                    foreach($user_list as $num => $value)
                    {
                        if(!$user_character_list = $this->tbl_character->get_character($value['tbl_account']['_id']))
                        {
                            $err_msg = 'NO USN-----';
                        }
                    }
                }*/
                if(!$user_character_list = $this->tbl_character->search_nickname($search_info['account']))
                {
                    $err_msg = LANG_USER_EMPTY;
                }
                else
                {
                    $num = 1 ;
                    foreach($user_character_list as $num => $value)
                    {
                        if(!$user_list = $this->tbl_account->search_id($value['tbl_character']['usn']))
                        {
                            $err_msg = 'NO USN-----';
                        }
                    }
                }
            }
            else
            {
                $err_msg = LANG_INPUT_ACCOUNT;
            }
        }

        $this->set(compact('user_list', 'user_character_list','search_info', 'err_msg'));
    }

    public function user_select()
    {
        /*$id = $this->passedArgs['id'];
        $user_data = $this->tbl_account->get_account($id);
        $user_character_list = $this->tbl_character->get_character($id);
        $user_billing_list = $this->tbl_billing_log->get_billing_list($id);

        if(!empty($this->passedArgs['char_serial']))
        {
            $char_serial = $this->passedArgs['char_serial'];
            $char_item_list = $this->__user_char_item($char_serial);
            $char_card_list = $this->__user_char_card($char_serial);
        }

        $this->set(compact(
            'user_data', 'user_character_list', 'user_billing_list','char_item_list',
            'char_card_list', 'char_serial'
        ));*/
    }

    public function private_message()
    {
        $send_message = '';
        $send_item = '';
        $char_usn = '';
        $result_code = -1 ;
        $result_msg = 'null';
        $result_data = '' ;
        $result_info = 'null';
        $url = '' ;
        $data = '';

        //if(!empty($this->data['send_message']))
        //{
        $send_message = $this->data['send_message'];
        foreach($this->data as $key=>$value)
        {
            if ( is_array($value))
            {
                echo $key. '<br />' ;
                foreach($value as $key2=>$value2)
                    echo $key2."=>".$value2. '<br />';
            }
            else
                echo $key."=>".$value. '<br />';
        }
        //echo $this->data ;
        //$search_info = $this->data['search_user'];

        $result_msg = 'send_message';
        if(!empty($send_message['message_info']))
        {
            $result_info = 'message_info' + $send_message['message_info'] ;

            $message_info = $send_message['message_info'] ;
            if (!empty($this->data['char_usn']))
            {
                $result_msg = 'char_usn' + $this->data['char_usn'] ;

                $char_usn = $this->data['char_usn'] ;

                App::uses('HttpSocket', 'Network/Http');
                $HttpSocket = new HttpSocket();

                $url = $this->server_url . '/notification_admin_msg';
                $data = array('target_id'=>$char_usn,'message'=>$message_info);
                $result_data = $data ;

                $result = json_decode($HttpSocket->post($url, $data), true);

                $result_code = $result['rcode'] ;
                @$result_msg = $result['message'] ;
                if($result['rcode'] == 0) {
                    $result_info = '쪽지 보내기 성공' ;
                } else {
                    $result_info = '쪽지 보내기 실패!! 인터넷 상태를 확인하세요.' ;
                }
            }
        }
        else
        {
            $result_info = 'send_message null';
        }

        //로그 설정
        $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>$url,'operation_data'=>$data,'operation_result'=>$result_info);
        $this->tbl_log->save($logdata);

        $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
    }

    public function send_item()
    {
        $send_item = '';
        $item_cnt = 0 ;
        $char_usn = '';
        $result_code = -1 ;
        $result_msg = 'null';
        $result_data = '' ;
        $result_info = 'null';

        $reward_type = '';
        $reward_id = '';

        $url = '';
        $data = '';

        $send_item = $this->data['send_item'];
        /*foreach($this->data as $key=>$value)
        {
            if ( is_array($value))
            {
                echo $key. '<br />' ;
                foreach($value as $key2=>$value2)
                    echo $key2."=>".$value2. '<br />';
            }
            else
                echo $key."=>".$value. '<br />';

            echo '-----<br />' ;
        }*/
        //echo $this->data ;
        //$search_info = $this->data['search_user'];

        $item_cnt = $send_item['item_cnt'] ;
        if(!empty($send_item['item_cnt']) && is_numeric($item_cnt))
        {
            $result_info = 'message_info' + $send_item['item_cnt'] ;

            $item_cnt = (int)$send_item['item_cnt'] ;
            if (!empty($this->data['char_usn']))
            {
                $result_msg = 'char_usn' + $this->data['char_usn'] ;

                $char_usn = $this->data['char_usn'] ;
                $item_type = $send_item['type'] ;

                App::uses('HttpSocket', 'Network/Http');
                $HttpSocket = new HttpSocket();
                $url = $this->server_url . '/notification_admin';

                if ( !empty($item_type) && is_numeric($item_cnt))
                {
                    $data = array('target_id'=>$char_usn,'reward_id'=>$item_type,'reward_type'=>$item_type,'count'=>$item_cnt,'message'=>__(LANG_ITEM_SEND_MESSAGE),'expire_hour'=>0);

                    $result_data = $data ;
                    $result = json_decode($HttpSocket->post($url, $data), true);

                    $result_code = $result['rcode'] ;
                    @$result_msg = $result['message'] ;
                    if($result['rcode'] == 0) {
                        $result_info = '아이템 지급 성공' ;
                    } else {
                        $result_info = '아이템 지급 실패!! 인터넷 상태를 확인하세요.' ;
                    }
                }
            }
        }
        else
        {
            $result_info = '--------------아이템 CNT 정확하게 입력하세요!!!!!!!!!!!!-----------------';

            $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
            return ;
        }

        //로그 설정
        $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>$url,'operation_data'=>$data,'operation_result'=>$result_info);
        $this->tbl_log->save($logdata);

        $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
    }

    public function user_select_edit()
    {
        if(!CakeSession::check('tbl_auth'))
        {
            $this->redirect(array(
                'controller' => 'User',
                'action' => 'user_list',
            ));
        }

        $id = $this->passedArgs['id'];
        $user_data = $this->tbl_account->get_account($id);
        $user_character_list = $this->tbl_character->get_character($id);

        if( !empty($this->data['edit_info']) )
        {
            $edit_info = $this->data['edit_info'];

            if( nullCheck($edit_info['gem']) )
            {
                $err_msg = __('Space is empty!');
            }
            else if( !numberCheck($edit_info['gem']) )
            {
                $err_msg = __("Numeric only!");
            }
            else
            {
                $user_data['tbl_account']['gem'] = $edit_info['gem'];
                $user_data['tbl_account']['block'] = $edit_info['block'];
                $this->tbl_account->save($user_data);

                if(!empty($this->passedArgs['char_serial']))
                {
                    if( nullCheck($edit_info['energy']) || nullCheck($edit_info['exp']) || nullCheck($edit_info['gold']) )
                    {
                        $err_msg = __('Space is empty!');
                    }
                    else if( !numberCheck($edit_info['energy']) || !numberCheck($edit_info['exp']) || !numberCheck($edit_info['gold']) )
                    {
                        $err_msg = __("Numeric only!");
                    }
                    else
                    {
                        $char_serial = $this->passedArgs['char_serial'];

                        $user_character['tbl_character']['a2x_id'] = $edit_info['a2x_id'];
                        $user_character['tbl_character']['char_serial'] = $char_serial;
                        $user_character['tbl_character']['gold'] = $edit_info['gold'];
                        $user_character['tbl_character']['energy'] = $edit_info['energy'];
                        $user_character['tbl_character']['exp'] = $edit_info['exp'];
                        $this->tbl_character->save($user_character);
                    }
                }

                $this->redirect(array(
                    'controller' => 'User',
                    'action' => 'user_select',
                    'id' => $id,
                    'search_value' => $this->passedArgs['search_value'],
                    'char_serial' => empty($char_serial) ? '' : $char_serial,
                ));
            }
        }

        $this->set(compact(
            'user_data', 'user_character_list', 'char_serial', 'err_msg'
        ));
    }

    public function slide_user_search()
    {
        $search_value = CakeSession::read('search_value');
        $user_list = $this->tbl_account->search_nickname($search_value);

        return $user_list;
    }

    private function __user_char_card($_char_serial)
    {
        $char_item_list = $this->tbl_card->get_card_list($_char_serial);
        return $char_item_list;
    }

    private function __user_char_item($_char_serial)
    {
        $char_item_list = $this->tbl_item->get_item_list($_char_serial);
        return $char_item_list;
    }

}